/**
 * 
 */
package org.mythflow.activity;

import org.mythflow.domain.Recording;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author dfrey
 *
 */
public class ProgramDetails extends Activity {

	private static final String TAG = "ProgramDetails";
	
	private TextView dateTextView;
	private TextView durationTextView;
	private TextView titleTextView;
	private TextView subTitleTextView;
	private TextView descriptionTextView;
	private Button playButton;
	
	private Intent intent;
	
	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		Log.i( TAG, "onCreate : enter" );
	
		setContentView( R.layout.program_details );
		
		findViews();
		updateFields( getIntent().getExtras() );
		setupListeners();
		
		Log.i( TAG, "onCreate : exit" );
	}
	
	private void findViews() {
		Log.v( TAG, "findViews : enter" );
		
		dateTextView = (TextView) findViewById( R.id.program_details_date );
		durationTextView = (TextView) findViewById( R.id.program_details_duration );
		titleTextView = (TextView) findViewById( R.id.program_details_title );
		subTitleTextView = (TextView) findViewById( R.id.program_details_sub_title );
		descriptionTextView = (TextView) findViewById( R.id.program_details_description );
		
		playButton = (Button) findViewById( R.id.program_details_play );
		
		intent = new Intent( this, ViewProgram.class );

		Log.v( TAG, "findViews : exit" );
	}

	private void setupListeners() {
		Log.v( TAG, "setupListeners : enter" );
		
		playButton.setOnClickListener( new View.OnClickListener() {
            public void onClick( View v ) {
                // Perform action on click
        		startActivity( intent );
            }
        });

		Log.v( TAG, "setupListeners : enter" );
	}
	
	private void updateFields( Bundle extras ) {
		Log.v( TAG, "updateFields : enter" );
		
		Recording recording = extras.getParcelable( "recording" );
		
		dateTextView.setText( recording.getStartTimeFormattedLong() );
		
		try {
			int length = Integer.parseInt( recording.getLength() );
			int minutes = (int) ( length / 60 );
			durationTextView.setText( minutes + " minutes" );
		} catch( NumberFormatException e ) {
			durationTextView.setText( "N/A" );
		}
		
		titleTextView.setText( recording.getTitle() );
		subTitleTextView.setText( recording.getSubTitle() );
		descriptionTextView.setText( recording.getDescription() );
		
		intent.putExtra( "filename", recording.getUrl() );
		intent.putExtra( "length", recording.getLength() );
		
		if( recording.isCurrentlyRecording() ) {
			playButton.setEnabled( false );
		}
		
		Log.v( TAG, "updateFields : exit" );
	}
	
}
